package view;

import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;

import game.ChessGame;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import chess.ChessClock;
import chess.Player;

public class ChessClockView {
	
	private static final long ONE_SECOND=1000;  // One thousand milliseconds
	
	private JLabel playerWtime;
	private JLabel playerBtime;
	
	private JPanel playerWpanel;
	private JPanel playerBpanel;
	
	private JFrame timerWindow;
	
	private ChessClock clock;
	
	private Player playerB;
	private Player playerW;

	private Timer clockUpdater;
	
	private boolean isClosed;
	
	/**
	 * Generate a new chess clock window for a chess game.
	 * 
	 * This window uses a Timer to update the clock display once a second.
	 * 
	 * @param game The chess game currently being played
	 */
	public ChessClockView(ChessGame game){
		this.timerWindow=new JFrame("Chess Clock");
		
		this.playerW=game.getPlayers().get(0);  //First player is white
		this.playerB=game.getPlayers().get(1);  //Second player is black
		
		this.clock=game.getClock();
		
		this.playerBtime=new JLabel(clock.getPlayerTime(playerB)) ;
		this.playerBtime.setForeground(Color.white);
		this.playerWtime=new JLabel(clock.getPlayerTime(playerW)) ;
		this.playerWtime.setForeground(Color.black);

		this.playerBpanel= new JPanel();
		this.playerBpanel.setBackground(Color.black);
		this.playerWpanel= new JPanel();
		this.playerWpanel.setBackground(Color.white);
		
		this.playerBpanel.add(playerBtime);
		this.playerWpanel.add(playerWtime);
		
		this.timerWindow.setSize(175,75);  //Make it as small as possible
		this.timerWindow.setAlwaysOnTop(true);
		this.timerWindow.setVisible(clock.isEnabled());
		this.timerWindow.setResizable(false);
		this.timerWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.timerWindow.setLocation(1100, 0);
		
		this.timerWindow.setContentPane(this.playerWpanel);
		updateLabelTime();
		isClosed = false;
		
		TimerTask updateClock = new TimerTask(){

			@Override
			public void run() {
				if (clock.isEnabled()){
					if(!timerWindow.isVisible() && timerWindow.getDefaultCloseOperation() != JFrame.HIDE_ON_CLOSE){
						timerWindow.setVisible(clock.isEnabled());
						timerWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
						isClosed = false;
					}
					//System.out.println("Updating");
					updateLabelTime();
				}else {
					if(timerWindow != null){
						timerWindow.setVisible(clock.isEnabled());
						timerWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
						isClosed = true;
					}
				}
				
			}
		};
		
		clockUpdater = new Timer();
		clockUpdater.scheduleAtFixedRate(updateClock, 0,ONE_SECOND);

	}

	/**
	 * Show the new player whose turn it is, and the time associated with the player
	 * @param currentPlayer The player whose turn it now is.
	 */
	public void switchPlayer(Player currentPlayer) {
		
		//System.out.println("Switching");
		if (currentPlayer.getColour()==ChessGame.WHITE){
			this.timerWindow.setContentPane(this.playerWpanel);
			//System.out.println("white ");
			
		}
		else{
			this.timerWindow.setContentPane(this.playerBpanel);
			//System.out.println("black");
		}
		updateLabelTime();
		this.timerWindow.validate();  //Forces a redraw
		this.timerWindow.repaint();
		
	}
	
	/**
	 * Grabs the new time for the appropriate player, and sticks it onto their label
	 */
	public void updateLabelTime(){
		if (clock.getCurPlayer()==playerW){
			//System.out.println("Getting White Clock Time");
			this.playerWtime.setText(clock.midturnClock()) ;
		}
		else{
			//System.out.println("Getting Black Clock Time");
			this.playerBtime.setText(clock.midturnClock()) ;
		}
	}
	
	/**
	 * Enables or disables the clock window.  
	 * @param status
	 */
	public void enabled (boolean status){
		//toggle for the clock, with be used to hide frame
		this.clock.enabled(status);

	}
	
	/**
	 * Lets you check whether or not the window is currently closed
	 * @return If the window is closed or not
	 * 
	 */
	public boolean isWindowClosed(){
		return isClosed;
	}
	/**
	 * This method, when called, will close the view
	 */
	public void closeView() {
		if(timerWindow != null){
			timerWindow.setVisible(false);
			clock.enabled(false);
			timerWindow = null;		
		}
	}
	
	/**
	 * Returns whether or not the clock is enabled
	 * @return if the clock is enabled
	 */
	public boolean isEnabled(){
		return this.clock.isEnabled();
	}
}
